/*
 * Copyright (c) 2017 Qunar.com. All Rights Reserved.
 */
package com.guoyu.project.web.api;

import java.util.Map;

import javax.annotation.Resource;

import com.guoyu.project.common.constant.Constants;
import com.guoyu.project.model.enums.ArticleStatus;
import com.guoyu.project.model.enums.CollegeType;
import com.guoyu.project.model.enums.ProgessCategory;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.Article;
import com.guoyu.project.model.enums.ArticleType;
import com.guoyu.project.model.enums.ProjectType;
import com.guoyu.project.service.ArticleService;
import com.guoyu.project.service.SchoolService;
import com.guoyu.project.web.admin.NewsController;

/**
 * @author: libo Date: 17-2-13 Time: 下午1:20
 * @version: \$Id$
 */
@Controller
@RequestMapping("api/project")
public class ProjectApiController {

    private final static Logger logger = LoggerFactory.getLogger(NewsController.class);

    @Resource
    private ArticleService articleService;
    @Resource
    private SchoolService schoolService;

    /**
     * 工程概况
     *
     * @return
     */
    @RequestMapping("news")
    @ResponseBody
    public APIResponse news(@RequestParam(defaultValue = "0") int offset, @RequestParam(defaultValue = "10") int limit,
            int projectId) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("majorType", ArticleType.PROJECT);
        params.put("minorType", ProjectType.NEWS);
        params.put("projectId", projectId);
        Pagination<Article> pagination = articleService.queryArticles(offset, limit, params);
        return APIResponse.returnSuccess(pagination);
    }

    @RequestMapping("tender")
    @ResponseBody
    public APIResponse tender(@RequestParam(defaultValue = "0") int offset,
            @RequestParam(defaultValue = "10") int limit) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("majorType", ArticleType.PROJECT);
        params.put("minorType", ProjectType.TENDER);
        Pagination<Article> pagination = articleService.queryArticles(offset, limit, params);
        return APIResponse.returnSuccess(pagination);
    }

    @RequestMapping("progress")
    @ResponseBody
    public APIResponse progress(@RequestParam(defaultValue = "-1") int category) {
        ProgessCategory progessCategory = ProgessCategory.codeOf(category);
        if (progessCategory == null) {
            return APIResponse.returnFail("类型:" + category + "错误");
        }
        Map<String, Object> params = Maps.newHashMap();
        params.put("majorType", ArticleType.PROGRESS);
        params.put("minorType", progessCategory);
        Pagination<Article> pagination = articleService.queryArticles(0, 1, params);
        if (CollectionUtils.isEmpty(pagination.getRows())) {
            return APIResponse.returnFail("无数据");
        }
        String picture = pagination.getRows().get(0).getPicture();
        String imageUrl = picture;
        if (StringUtils.isNotBlank(picture)) {
            imageUrl = Constants.DOMAIN + picture;
        }
        return APIResponse.returnSuccess(imageUrl);
    }

    @RequestMapping("over")
    @ResponseBody
    public APIResponse over(@RequestParam(defaultValue = "0") int offset,
            @RequestParam(defaultValue = "10") int limit) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("majorType", ArticleType.PROJECT);
        params.put("minorType", ProjectType.PROJECT);
        params.put("status", ArticleStatus.OVER);
        params.put("needSchool", Boolean.TRUE);
        Pagination<Article> pagination = articleService.queryArticles(offset, limit, params);
        return APIResponse.returnSuccess(pagination);
    }

}
